/**
 * Copyright 2015 Software innovation and R & D center. All rights reserved.
 * File Name: jdbcDemo.java
 * Encoding UTF-8
 * Version: 0.0.1
 * History:	2017年1月12日
 */
package JDBC;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.junit.Test;

import TOOL.jdbcTool;

/**
 * 
 * @author: qun.xie
 * @version Revision: 0.0.1
 * @Date: 2017年1月12日
 */
public class jdbcDemo {
	@Test
	public void showTransaction() throws SQLException{
		Connection conn = null;
		PreparedStatement psmt = null;
		ResultSet rs = null;
		
		try {
		conn = jdbcTool.getConnection();
		conn.setAutoCommit(false); //开启事务
			String sql ="";    //sql语句
			//sql = "select * from bankp where username=?";
			//sql = "insert into bankp values(?,?)";
			sql = "update bankp set money = money + ? where username = ?";
			/*
			 * 获得预处理对象
			 */
			psmt = conn.prepareStatement(sql);
			/*
			 * 设置实际参数
			 */
			
			//psmt.setString(1, "jack");
			//psmt.setInt(2, 1000);
			/*
			 * 执行
			 */
			int r=0;
			/**事务演示*/
			psmt.setInt(1, 100);
			psmt.setString(2, "rose");
			r = psmt.executeUpdate();
			System.out.println(r);
			
			//模拟异常
			int i = 0;
			i= i/0;
			
			psmt.setInt(1, -100);
			psmt.setString(2, "jack");
			 r = psmt.executeUpdate();
			System.out.println(r);
			
		} catch (SQLException e) {
			/**程序异常，事务回滚*/
			if (conn != null) {
				conn.rollback();
			}
			
			e.printStackTrace();
		}finally{
			jdbcTool.close(conn, psmt, rs);
		}
		
		
	}
}
